package com.xbjtech.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;

import com.excel.poi.util.PoiAnalyticalData;
import com.excel.poi.util.PoiUtil;
import com.xbjtech.model.Order;

public class ListToExcel {
	
	@Test
	public void testListToExcel() throws IllegalArgumentException, IllegalAccessException, InstantiationException, IOException {
		List<Object> list = new LinkedList<>(); 
		for (int i = 0; i < 100; i++) {
			Order o = new Order();
			o.setId(i);
			o.setBuyerName("李" + i);
			o.setBuyerMobile("1390101" + i);
			list.add(o);
		}
		// xls 的excel对象
		// xls    HSSFWorkbook
		// xlsx   XSSFWorkbook
		HSSFWorkbook workbook = PoiUtil.createWorkbook("工作表1", Order.class, list);
		FileOutputStream fs = new FileOutputStream("src/test/java/a.xls");
		try {
			workbook.write(fs);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		fs.flush();
		
	}
	
	@Test
	public void testExcelToList() throws IOException, IllegalArgumentException, IllegalAccessException, InstantiationException {
		String file = "src/test/java/a.xls";
		FileInputStream fileInputStream = new FileInputStream(file);
		HSSFWorkbook wb = new HSSFWorkbook(fileInputStream);
		List<Object> analyticPoiExcel = PoiAnalyticalData.analyticPoiExcel(wb, Order.class);
		for (Object o : analyticPoiExcel) {
			System.out.println(o);
		}
	}

}
